package khs.java_mysql.main.java;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException
    {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        
        System.out.print("server: ");
        String server = bufferedReader.readLine();
        
        System.out.print("port: ");
        String port = bufferedReader.readLine();
        
        System.out.print("database: ");
        String database = bufferedReader.readLine();
        
        System.out.print("userid: ");
        String userid = bufferedReader.readLine();
        
        System.out.print("password: ");
        String password = bufferedReader.readLine();
        
        System.out.print("query: ");
        String query = bufferedReader.readLine();
        
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        
        Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://" + server + ":" + port + "/" + database, userid, password);
        Statement stmt = null;
        ResultSet rs = null;
        
        stmt = (Statement) conn.createStatement();
        rs = stmt.executeQuery(query);
        
        ResultSetMetaData rsmetaData = rs.getMetaData();
        int numberOfColumns = rsmetaData.getColumnCount();
        
        while(rs.next()){
            for(int i=1;i<=numberOfColumns;i++){
                System.out.print(rs.getObject(i));
                System.out.print("\t");
            }
            System.out.print("\n");
        }
    }
}
